package cn.lyq.card.ui.qualified

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.model.CashBackStageWrapInfo
import cn.lyq.card.utils.IntToSmallChineseNumber
import cn.lyq.card.utils.bindViewPager
import cn.lyq.card.utils.setSimpleData
import kotlinx.android.synthetic.main.fg_course.*
import kotlinx.android.synthetic.main.layout_tab_normal.*
import kotlinx.android.synthetic.main.layout_title.*


class QualifiedStoreActivity : BaseActivity() {


    private val mFragments = mutableListOf<BaseFg>()
    private val mTitles = mutableListOf<String>()
    private val mAdapter by lazy {
        MyPagerAdapter(supportFragmentManager)
    }
    private val mInfo by lazy {
        intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is CashBackStageWrapInfo }
            ?.run { this as CashBackStageWrapInfo }
    }
    private val mGoodsId by lazy {
        mInfo?.goodsId ?: 0
    }
    private val mTarget by lazy {
        intent.getIntExtra(KEY_TARGET, 0)
    }
    private val mMax by lazy {
        mInfo?.list?.size ?: 0
    }

    companion object {

        private const val KEY_INFO = "INFO"
        private const val KEY_TARGET = "TARGET"

        @JvmStatic
        fun start(context: Context, info: CashBackStageWrapInfo, target: Int) {
            val starter = Intent(context, QualifiedStoreActivity::class.java)
                .putExtra(KEY_INFO, info)
                .putExtra(KEY_TARGET, target)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fg_course
    }

    override fun initView() {
        super.initView()
        tvTitleSplit.visibility = View.GONE
        tvTitle.text = "${mInfo?.title ?: "机具"}达标商户"

        mTitles.clear()
        mFragments.clear()

        for (i in 0 until mMax) {
            mTitles.add("第${IntToSmallChineseNumber.ToCH(i + 1)}阶段")
            mFragments.add(QualifiedStoreFg.newInstance(i, mGoodsId))
        }
        viewPager.adapter = mAdapter
        if (mTitles.size > 4) {
            slidingTab.setViewPager(viewPager, mTitles.toTypedArray())
            tabLayout.visibility = View.GONE
        } else {
            tabLayout.setSimpleData(mTitles.toTypedArray())
            tabLayout.bindViewPager(viewPager)
            slidingTab.visibility = View.GONE
        }
        if (mTarget != 0) {
            viewPager.setCurrentItem(mTarget, false)
        }
    }

    inner class MyPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getCount(): Int {
            return mTitles.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitles[position]
        }

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }
    }
}